<?php
    /**
     * @name 日志类
     * @copyright tcsos inc
     * @version cooperation v1.0
     */

    class log {

        //日志保存路径
        private $path;

        //日志等级
        private $level;

        /**
         * 构造函数
         * 
         * @param string $path  日志保存路径
         * @param int $level    日志等级
         * @return void
         */
        public function __construct($path, $level = 0) {
            if(!$path) {
                exit('log write path error.');
            }
            if(($level = intval($level)) < 0 || $level > 2) {
                $level = 0;
            }

            $this->path = $path;
            $this->level = $level;
        }

        /**
         * 写入日志
         * 
         * @param string $msg   错误信息
         */
        public function write_log($msg , $level) {
            //如果日志是关闭的
            if($this->level < 1) {
                return false;
            }
            //当level开启为1时候，过滤掉多余的>1的日志
            if($this->level == 1 && $level >1){
                return false;
            }

            //时间
            $time = date('Y-m-d H:i:s');
            //文件
            $file = $this->path . date('Ym') . '_errorlog.php';
            //SESSION
            $session = serialize($_SESSION);            
            //COOKIE
            $cookie = serialize($_COOKIE);
            //IP
            $ip = get_remote_ip();     
            
            $str = str_repeat('-=', 50);

            $msg = "{$str}\ntime:{$time}\n{$msg}\nsession:{$session}\ncookie:{$cookie}\nip:{$ip}\n";
                                    
            $handle = fopen($file, 'a');
            if($handle){
                fwrite($handle, $msg);
                fclose($handle);
            }            
        }
    }
?>